/* Copyright (C) 2012 Nanakos Chrysostomos - <cnanakos@ekt.gr>
   National Documentation Centre
   dPool Elasic Cluster - Distributed Image Converting System
   dPool is placed under the GNU General Public License, version 3 or later.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
var LDAP = require('LDAP');
var ldap = new LDAP({ uri: 'ldap://AD_server_or_IP_addr:389', version: 3 }); //Use your own LDAP Server

module.exports.authenticate = function(login, password, callback) {
  if(password.length ==0)
  {
	callback(null);
	return;
  }
  var user = {login: login, password: password, role: 'admin'};
  var bind_options = {
    binddn: "YOURDOMAIN\\"+login,
    password: password
    };


ldap.open(function(err) {
    if (err) {
       return;
    }
    });

ldap.simpleBind(bind_options, function(err) {
        if(err){
                ldap.close();
                callback(null);
                return;
        } else {
                ldap.close();
                callback(user);
                return;
        }
   });

};
